package com.example.lss.news;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.lss.news.adapter.ReAdapter;
import com.example.lss.news.adapter.SearchReAdapter;
import com.example.lss.news.bean.Data;
import com.example.lss.news.bean.Ress;
import com.example.lss.news.bean.Result;
import com.example.lss.news.utils.JsonUtils;
import com.example.lss.news.utils.app.BaseActivity;
import com.example.lss.news.utils.http.HttpUtils;
import com.example.lss.news.utils.http.JsonCallback;

import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends BaseActivity implements View.OnClickListener {

    private ImageView img_fh, img_search;
    private EditText et_search;
    private RecyclerView rv_search;
    SearchReAdapter reAdapter;
    private Ress<Result<List<Data>>> r = null;
    private String type;

    @Override
    protected int getLayoutId() {
        return R.layout.act_search;
    }

    @Override
    protected void initView() {
        img_fh = findViewById(R.id.img_fh);
        img_search = findViewById(R.id.img_search);
        et_search = findViewById(R.id.et_search);
        rv_search = findViewById(R.id.rv_search);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initListener() {
        img_search.setOnClickListener(this);
        img_fh.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == img_fh) {
            finish();
        } else if (v == img_search) {
            new Thread() {

                @Override
                public void run() {
                    if (et_search.getText().toString().equals("财经")) {
                        type = "caijing";
                    } else if (et_search.getText().toString().equals("时尚")) {
                        type = "shishang";
                    } else if (et_search.getText().toString().equals("国际")) {
                        type = "guoji";
                    }
                    HttpUtils.get("http://v.juhe.cn/toutiao/index?type=" + type + "&key=9980cb57a9269e256bcc015d498dd572", new JsonCallback() {
                        @Override
                        public void onStart(Object tag) {

                        }

                        @Override
                        public void onSuccess(final String json) {


                            r = JsonUtils.toBean(json, JsonUtils.newParamType(Ress.class, JsonUtils.newParamType(Result.class, JsonUtils.newParamType(List.class, Data.class))));

                            if (r.getReason().equals("成功的返回")) {
                                List<Data> list = new ArrayList<>();
                                list.addAll(r.getResult().getData());
                                reAdapter = new SearchReAdapter(list);
                                rv_search.setLayoutManager(new LinearLayoutManager(act));
                                rv_search.setAdapter(reAdapter);
                            }
                        }

                        @Override
                        public void onError(Throwable exception) {

                        }

                        @Override
                        public void onFinish() {

                        }
                    });
                }
            }.start();
        }
    }
}
